﻿@model LeadMaster.ViewModels.ViewModelScheduleSections

@{
    ViewBag.Title = "Edit";
}
<div class="spacer"></div>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Edit A Schedule Section</legend>
        <div class="spacer"></div>
        @Html.HiddenFor(model => model.ScheduleSection.ScheduleSectionsID)

        <div class="editor-label">
            @Html.LabelFor(model => model.ScheduleSection.FieldHeader)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.ScheduleSection.FieldHeader, new { @class = "largetextbox" })
            @Html.ValidationMessageFor(model => model.ScheduleSection.FieldHeader)
        </div>
        <div class="spacer"></div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ScheduleSection.Content)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.ScheduleSection.Content, new { @class = "notes" }) &nbsp;
            @Html.DropDownListFor(model => model.holdingSectionMerge, new SelectList(Model.SectionMerges, "Markup", "Description"), new { })
            <input type="button" id="buttonMerge" value="Insert Merge Field" />
            @Html.ValidationMessageFor(model => model.ScheduleSection.Content)
        </div>
        <div class="spacer"></div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
<input type="hidden" id="hdnCursorPosition" value="0" />
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

<script type="text/javascript">
    $(document).ready(function () {

        $("#buttonMerge").click(function () {
            var newText = $("#ScheduleSection_Content").val().substring(0, $("#hdnCursorPosition").val());
            newText += $("#holdingSectionMerge").val() ;
            newText += $("#ScheduleSection_Content").val().substring($("#hdnCursorPosition").val());
            $("#ScheduleSection_Content").val(newText);
        });

        $("#ScheduleSection_Content").focusout(function () {
            var posInBox = $("#ScheduleSection_Content").getCursorPosition();
            $("#hdnCursorPosition").val(posInBox.toString());
        });



        (function ($, undefined) {
            $.fn.getCursorPosition = function () {
                var el = $(this).get(0);
                var pos = 0;
                if ('selectionStart' in el) {
                    pos = el.selectionStart;
                } else if ('selection' in document) {
                    el.focus();
                    var Sel = document.selection.createRange();
                    var SelLength = document.selection.createRange().text.length;
                    Sel.moveStart('character', -el.value.length);
                    pos = Sel.text.length - SelLength;
                }
                return pos;
            }
        })(jQuery);

    });
</script>
